package com.xdk.templatescala.dao;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.xdk.templatescala.common.constants.ScaleContant;
import com.xdk.templatescala.common.enums.ScaleStatusEnum;
import com.xdk.templatescala.common.response.Page;
import com.xdk.templatescala.model.ScaleEntry;
import com.xdk.templatescala.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component
public class ScaleDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 获取所有的工作表
     *
     * @return
     */
    public Page<ScaleEntry> findAll(Integer pageNum, Integer pageSize) {
        //分页查询
        if (pageNum > 0){
            pageNum = pageNum - 1;
        }
        //创建排序规则
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
        Pageable pageable = PageRequest.of(pageNum , pageSize);
        Query query = new Query();
        query.addCriteria(Criteria.where(ScaleContant.STATUS).is(ScaleStatusEnum.SCALE_STATUS_1.getCode()));
        query.with(sort);
        Long count = mongoTemplate.count(query, ScaleEntry.class);
        query.with(pageable);
        List<ScaleEntry> list = mongoTemplate.find(query, ScaleEntry.class);
        return Page.build(pageNum, pageSize, count, list);
    }

    /**
     * 根据scaleId获取模板
     *
     * @return
     */
    public ScaleEntry findOne(String scaleId) {
        Query query = Query.query(Criteria.where(ScaleContant.SCALE_ID).is(scaleId));
        return mongoTemplate.findOne(query,ScaleEntry.class);
    }

    /**
     * 根据scaleIds删除模板
     *
     * @return
     */
    public Long delScales(String scaleIds) {
        String[] ids = scaleIds.split(",");
        Query query = Query.query(Criteria.where(ScaleContant.SCALE_ID).in(Arrays.asList(ids)));
        return mongoTemplate.remove(query, ScaleEntry.class).getDeletedCount();
    }

    /**
     * 根据scaleId编辑模板
     *
     * @return
     */
    public Long updateScales(ScaleEntry scaleEntry) {
        Query query = Query.query(Criteria.where(ScaleContant.SCALE_ID).is(scaleEntry.getScaleId()));
        Update update = new Update();
        update.set(ScaleContant.SCALE_JSON, scaleEntry.getScaleJson());
        update.set(ScaleContant.UPDATE_TIME, DateUtils.getTime());
        if(!ObjectUtil.isEmpty(scaleEntry.getScaleName())){
            update.set(ScaleContant.SCALE_NAME, scaleEntry.getScaleName());
        }
        return mongoTemplate.updateFirst(query, update, ScaleEntry.class).getModifiedCount();
    }

    /**
     * 添加模板量表
     *
     * @param scaleEntry
     */
    public void addScaleEntry(ScaleEntry scaleEntry) {
        mongoTemplate.save(scaleEntry);
    }
}
